import Vue, { getCurrentInstance } from 'vue';

export const useCurrentInstance = (): [Vue, Function] => {
  const { proxy: currentCtx } = getCurrentInstance() || {};

  const setData = (key: string, value: any) => {
    currentCtx && (currentCtx.$data[key] = value);
  };

  return [currentCtx as unknown as Vue, setData];
};

export const useRouter = () => {
  const [ctx] = useCurrentInstance();
  const query = ctx && ctx?.$route.query;
  const params = ctx && ctx?.$route.params;
  const router = ctx && ctx.$router;
  const route = ctx && ctx.$route;
  return {
    router,
    route,
    query,
    params,
  };
};
